﻿var actionURL = "ashx/DataTestCBDHandler.ashx";
var devtype = [{ "id": "all", "text": "所有" }, { "id": "er", "text": "损坏设备", "selected": true }, { "id": "ok", "text": "正常设备" }];
var uid;
var cid = "";
var hDialog;
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
var mylayout = {
    init: function (size) {
        $('#layout').width(size.width - 4).height(size.height - 4).layout();
        var center = $('#layout').layout('panel', 'center');
        center.panel({
            onResize: function (w, h) {
                $('#histList').datagrid('resize', { width: w, height: h });
            }
        });
    },
    resize: function (size) {
        mylayout.init(size);
        $('#layout').layout('resize');
    }
};

$(function () {
    var size = { width: $(window).width(), height: $(window).height() };
    mylayout.init(size);
    uid = getUrlParam("uid");
    $('#dtOpstart,#dtOpend').datebox({ width: 100 });
    DicCategory.bindTree();
    
    autoResize({ dataGrid: '#histList', gridType: 'datagrid', callback: grid.bind, height: 0 });
    $('#a_search').click(search);
    $('#a_export').click(function () {
        if (cid != "") {
            var ee = new ExportExcel('histList');
            ee.go("t_cbdstatus");
        }
        else {
            msg.error('对不起，请选择设备再导出！');
        }
    });
    $('#a_send').click(CRUD.show);
});

var DicCategory = {
    bindTree: function () {       
        $('#cdbNum').tree({
            url: '/status_cbd/ashx/DataTestCBDHandler.ashx?Action=cbdNums&uid=' + uid,
            onLoadSuccess: function (node, data) {
                if (data.length == 0) {
                    $('#noCategoryInfo').fadeIn();
                }
                $('body').data('categoryData', data);
            },
            onClick: function (node) {
                var cc = node.id;
                cid = cc;
                var ruleArr = [];
                dtOpstart = $('#dtOpstart').datebox('getValue');
                dtOpend = $('#dtOpend').datebox('getValue');
                if (cid != "") {
                    ruleArr.push({ "field": "devID", "op": "eq", "data": cid });
                }
                if (dtOpstart != '')
                    ruleArr.push({ "field": "updateTime", "op": "ge", "data": dtOpstart });
                if (dtOpend != '')
                    ruleArr.push({ "field": "updateTime", "op": "le", "data": dtOpend });
                if (ruleArr.length > 0) {
                    filterObj = { groupOp: 'AND', rules: ruleArr };
                    $('body').data('where', JSON.stringify(filterObj));
                }
                $('#histList').datagrid({
                    title: "设备[" + node.attributes.username + "] 的历史记录",
                    url: '/status_cbd/ashx/DataTestCBDHandler.ashx?Action=detail',
                    queryParams: { devID: cc }
                });
            }
        });
    },
    reload: function () {
        $('#cdbNum').tree('reload');
    },
    getSelected: function () {
        return $('#cdbNum').tree('getSelected');
    }
};

var grid = {
    bind: function (winSize) {
        $('#histList').datagrid({
            toolbar: '#toolbar',
            title: "测报灯历史数据列表",
            iconCls: 'icon icon-list',
            width: winSize.width-200,
            height: winSize.height,
            nowrap: true, //折行
            rownumbers: true, //行号
            striped: true, //隔行变色
            idField: 'keyid', //主键
            singleSelect: false, //单选
            selectOnCheck: true,
            checkOnSelect: true,
            columns: [[
                { title: 'keyid', field: 'keyid', width: 90, align: 'center', checkbox: 'true' },
                { title: '设备ID', field: 'devID', width: 120, align: 'center' },
                { title: '灯管', field: 'sta_ch0', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch0 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    }
                },
                { title: '加热1', field: 'sta_ch1', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch1 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    } 
                },
                { title: '加热2', field: 'sta_ch2', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch2 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    } 
                },
                { title: '翻板', field: 'sta_ch3', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch3 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    } 
                },
                { title: '接雨', field: 'sta_ch4', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch4 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    } 
                },
                { title: '圆盘', field: 'sta_ch5', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch5 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    } 
                },
                { title: '设备地点', field: 'DevLocation', width: 150, align: 'center' },
                { title: '所有人', field: 'DevOwnerName', width: 150, align: 'center' },
                { title: '更新时间', field: 'updateTime', width: 150, align: 'center' }
            ]],
            pagination: true,
            pageSize: PAGESIZE
        });
    },
    getSelectedRow: function () {
        return $('#histList').datagrid('getSelected');
    },
    reload: function () {
        $('#histList').datagrid('clearSelections').datagrid('reload', { filter: '' });
    }
};

function createParam(action, keyid) {
    var o = {};
    var query = top.$('#uiform').serializeArray();
    query = convertArray(query);
    o.jsonEntity = JSON.stringify(query);
    o.action = action;
    o.keyid = keyid;
    return "json=" + JSON.stringify(o);
}
function search() {
    dtOpstart = $('#dtOpstart').datebox('getValue');
    dtOpend = $('#dtOpend').datebox('getValue');
    var ruleArr = [];
    if (cid != "") {
        ruleArr.push({ "field": "devID", "op": "eq", "data": cid });
    }
    if (dtOpstart != '')
        ruleArr.push({ "field": "updateTime", "op": "ge", "data": dtOpstart });
    if (dtOpend != '')
        ruleArr.push({ "field": "updateTime", "op": "le", "data": dtOpend });
    if (ruleArr.length > 0) {
        filterObj = { groupOp: 'AND', rules: ruleArr };
        $('body').data('where', JSON.stringify(filterObj));
        $('#histList').datagrid('load', { filter: JSON.stringify(filterObj), devID: cid });
    } else {
        grid.reload();
    }
}
var CRUD = {    
    show: function () {
        var checkedItems = $('#histList').datagrid('getChecked');
        if (cid== "") {
            msg.error('对不起，请选择设备！');
            return;
        }
        if (checkedItems == "") {
            msg.error('对不起，请选择发送信息！');
            return;
        }
        hDialog = $("#wdialog").dialog({
            title: '发送信息', width: 380, height: 360,            
            iconCls: '',
            onOpen: function () {
                $("#cc").combobox({
                    url: '/status_cbd/ashx/DataTestCBDHandler.ashx?Action=getUserRoleName',
                    valueField: 'KeyId',
                    textField: 'RoleName'
                });
                $("#cc").combobox({
                    onLoadSuccess: function (data) {
                        var selectData = $("#cc").combobox('getData');                        
                        if (selectData != null) {
                            $("#cc ").combobox('select', selectData[0].KeyId);
                        }                      

                    },
                    onSelect: function () {
                        $('#cct').combobox({
                            url : '/status_cbd/ashx/DataTestCBDHandler.ashx?Action=getUserInfo&roleId='
                                + $('#cc').combobox('getValue'),
                            valueField: 'UserID',
                            textField: 'TrueName',
                            multiple: "true"
                        });
                      
                    }
                });
               
                var checkedStr = "";
                jQuery.each(checkedItems, function (i, d) {
                    checkedStr += "设备ID：" + d.devID;
                    if (d.sta_ch0 == "0") {
                        checkedStr += "，灯管、";
                    }
                    if (d.sta_ch1 == "0") {
                        checkedStr += "，加热1、";
                    }
                    if (d.sta_ch2 == "0") {
                        checkedStr += "，加热2、";
                    }
                    if (d.sta_ch3 == "0") {
                        checkedStr += "，翻板、";
                    }
                    if (d.sta_ch4 == "0") {
                        checkedStr += "，接雨、";
                    }
                    if (d.sta_ch5 == "0") {
                        checkedStr += "，圆盘";
                    }
                    checkedStr += "工作不正常，更新时间：" + d.updateTime +",";
                    
                });

                checkedStr = checkedStr.substring(0, checkedStr.lastIndexOf(","));

                $('#sendcontent').val(checkedStr);
            },
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: sendmsg
            }, {
                text: '关闭',
                iconCls: 'icon-cancel',
                handler: function () {
                   hDialog.dialog('close');
                }
            }]            
        });
    }
};

function sendmsg() {
    
    var userids = $("#cct").combotree('getValues');
    var content = $("#sendcontent").val();
    if (userids == "") {
        msg.error("请选择客服人员！");
        return;
    }
    $('#sendfrm').form('submit', {
        url: '/status_cbd/ashx/DataTestCBDHandler.ashx',
        type:'POST',
        onSubmit: function (param) {
            var isValid = $('#sendfrm').form('validate');
            if (isValid) {               
                param.Action = "add";//这里一定要写上参数，否则找不到方法add
                param.uinfo = userids;
                param.cinfo = content;
                param.senduid = uid;
                msg.ok('正在发送中...');
                return true;
            }
            else {
                return false;
            }
        },        
        success: function (data) {            
            var d = eval('(' + data + ')');
            if (d.Success)
            {
                hDialog.dialog('close');
                msg.ok('发送成功');                
            }               
            else {
                msg.error("发送失败");
            }
        }
    });

   

}

